//
//  HistoryController.swift
//  ExpressDriver
//
//  Created by sheng on 2017/11/15.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import SnapKit
import MJRefresh

class HistoryController: UIViewController,UITableViewDataSource,UITableViewDelegate, OrderCellDelegate {
    
    
    var completeLabel: UILabel!
    
    var outstandLabel: UILabel!
    
    var orderNoTextField: UITextField!
    
    var horizontalLine: UIView!
    
    let freshHeader = MJRefreshNormalHeader()
    
    let freshFooter = MJRefreshBackNormalFooter()
    
    var orderTabview: UITableView!
    let statusHeight = UIApplication.shared.statusBarFrame.height
    let scrrenHeight = UIScreen.main.bounds.height
    let scrrenWidth = UIScreen.main.bounds.width
    

    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    func setUI() {
        self.view.backgroundColor = UIColor.white
        let navigHeight = self.navigationController!.navigationBar.frame.height
        
        completeLabel = UILabel()
        completeLabel.text = "complete 100"
        completeLabel.textColor = UIColor.black
        completeLabel.font = UIFont.systemFont(ofSize: 13)
        self.view.addSubview(completeLabel)
        completeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(statusHeight + navigHeight + 10)
            make.left.equalTo(10)
        }
        
        outstandLabel = UILabel()
        outstandLabel.text = "oustanging 20"
        outstandLabel.textColor = UIColor.red
        outstandLabel.font = UIFont.systemFont(ofSize: 13)
        self.view.addSubview(outstandLabel)
        outstandLabel.snp.makeConstraints { (make) in
            make.top.equalTo(completeLabel.snp.bottom).offset(2)
            make.left.equalTo(completeLabel.snp.left)
        }
        
        orderNoTextField = UITextField()
        orderNoTextField.borderStyle = UITextBorderStyle.roundedRect
        orderNoTextField.font = UIFont.systemFont(ofSize: 14)
        self.view.addSubview(orderNoTextField)
        orderNoTextField.snp.makeConstraints { (make) in
            make.top.equalTo(completeLabel.snp.top)
            make.right.equalTo(-10)
            make.width.equalTo(150)
        }
        
        
        horizontalLine =  UIView()
        horizontalLine.backgroundColor = UIColor.lightGray
        self.view.addSubview(horizontalLine)
        horizontalLine.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(outstandLabel.snp.bottom).offset(10)
            make.height.equalTo(0.5)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.centerX.equalTo(self.view)
        }
        
        
        orderTabview = UITableView();
        self.orderTabview.dataSource = self
        self.orderTabview.delegate = self
        let nib = UINib(nibName: String(describing: OrderCellTableViewCell.self), bundle: nil)
        self.orderTabview.register(nib, forCellReuseIdentifier: "orderItem")
        self.orderTabview?.estimatedRowHeight = 44.0
        self.orderTabview?.rowHeight = UITableViewAutomaticDimension
        self.view.addSubview(orderTabview)
        
        orderTabview.snp.makeConstraints { (make) in
            make.top.equalTo(horizontalLine.snp.bottom)
            make.left.right.bottom.equalTo(0)
        }
        
       
        freshHeader.setTitle("Pull to refresh", for: .idle)
        freshHeader.setTitle("Release to refresh", for: .pulling)
        freshHeader.setTitle("Loading...", for: .refreshing)
        //header.lastUpdatedTimeLabel.isHidden = true
        
        //下拉刷新
        freshHeader.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        orderTabview!.mj_header = freshHeader
        
        freshFooter.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        //freshFooter.isRefreshingTitleHidden = true
        freshFooter.stateLabel.isHidden = true
        orderTabview.mj_footer = freshFooter
    }
    
    func headerRefresh(){
        sleep(2)
        //重现加载表格数据
        orderTabview!.reloadData()
        //结束刷新
        orderTabview!.mj_header.endRefreshing()
    }
    
    func footerRefresh() {
        sleep(2)
        orderTabview.mj_footer.endRefreshing()
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "orderItem", for: indexPath) as! OrderCellTableViewCell
        cell.selectionStyle = .none
        cell.delegate = self
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("第\(indexPath.row)行被点击了")
    }
    
    
    
    func viewOrderDetail(label: UILabel) {
        
        let orderController = OrderDetailController();
        orderController.title = "Detail"
        self.navigationController?.pushViewController(orderController, animated: true)
    }
    
    
    func printOrder(label: UILabel) {
        let printController = PrintController();
        printController.title = "Print"
        self.navigationController?.pushViewController(printController, animated: true)
    }
    
    func signatureOrder(label: UILabel) {
        let signatureController = SignatureController()
        signatureController.title = "Signature"
        self.navigationController?.pushViewController(signatureController, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   

}
